package cc.admin.modules.wea.utils;

import cc.admin.modules.wea.entity.WeatherConstant;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author feng
 * @version V1.0
 * @Package com.example.weatherapitest.utils
 * @date 2024/8/27 8:34
 */
public class WeatherUtil {

    private static HashMap<WeatherConstant,Object> weatherURL = new HashMap<>();
    private static List<WeatherConstant> setCategory = new ArrayList<>();
    private static List<WeatherConstant> setLocation = new ArrayList<>();
    private static List<WeatherConstant> setSpaceAndBegin = new ArrayList<>();
    private static List<WeatherConstant> setEventId = new ArrayList<>();
    private static List<WeatherConstant> setYear = new ArrayList<>();
    private static List<WeatherConstant> setCode = new ArrayList<>();

    /**
     * Token
     */
    private static final String Token = "f8687123b57464ec719e2a3197c694e7";

    public static String getWeather(WeatherConstant type, String... data) {
        HashMap<String,Object> map = new HashMap<>();
        if (setCategory.contains(type)){
            map.put("category", data[0]);
        }

        if (setLocation.contains(type)) {
            map.put("location", data[0]);
        }

        if (setEventId.contains(type)){
            map.put("eventID", data[0]);
        }

        if (setYear.contains(type)){
            map.put("year", data[0]);
        }

        if (setCode.contains(type)){
            map.put("code", data[0]);
        }

        if (setSpaceAndBegin.contains(type)){
            map.put("location", data[0]);
        }

        return getWeather(type, map);
    }


    private static String getWeather(WeatherConstant type,HashMap<String,Object> paramMap){
       String data = null;
       try {
            paramMap.put("version","1.0");
            paramMap.put("forecastOrg", "中国");
            String result = HttpRequest.get((String) weatherURL.get(type))
                   .form(paramMap)
                   .header("Token",Token)
                   .execute().body();
            data = JSONObject.parseObject(result).getString("data");

       }catch (Exception e){
           e.printStackTrace();

       }
       return  data;
    }

    //URL存放
    static {
        weatherURL.put(WeatherConstant.WAVE_WARNING, "https://api.foreocean.com/warning/seaWave");
        weatherURL.put(WeatherConstant.STORM_WARNING, "https://api.foreocean.com/warning/stormTide");
        weatherURL.put(WeatherConstant.EXTREME_WEATHER_WARNING, "https://api.foreocean.com/warning/weather");
        weatherURL.put(WeatherConstant.SEVEN_DAY_FORECAST, "https://api.foreocean.com/weather/daily/7d");
        weatherURL.put(WeatherConstant.SEA_ICE_WARNING, "https://api.foreocean.com/warning/seaIce");
        weatherURL.put(WeatherConstant.TSUNAMI_WARNING_LIST, "https://api.foreocean.com/warning/lastesTsunamiEvents");
        weatherURL.put(WeatherConstant.TSUNAMI_WARNING_DETAIL, "https://api.foreocean.com/warning/tsunami");
        weatherURL.put(WeatherConstant.TROPICAL_CYCLONE, "https://api.foreocean.com/typhoon/station/list");
        weatherURL.put(WeatherConstant.SEVENTY_TWO_HOURS, "https://api.foreocean.com/environment/universal/hourly/72h");
        weatherURL.put(WeatherConstant.SEVENTY_TWO_HOURS_TIDAL, "https://api.foreocean.com/environment/tide/hourly/72h");
        weatherURL.put(WeatherConstant.SEA_CONDITIONS,"https://api.foreocean.com/weather/realtime");
        weatherURL.put(WeatherConstant.SEA_FOG,"https://api.foreocean.com/ocean/seafog");

        weatherURL.put(WeatherConstant.TROPICAL_CYCLONE_YEAR,"https://api.foreocean.com/typhoon/list");
        weatherURL.put(WeatherConstant.TROPICAL_CYCLONE_FORECAST,"https://api.foreocean.com/typhoon/forecast");
        weatherURL.put(WeatherConstant.TROPICAL_CYCLONE_TRACK,"https://api.foreocean.com/typhoon/track");
        /**
         * 一个参数
         */
        setCategory.add(WeatherConstant.WAVE_WARNING);
        setCategory.add(WeatherConstant.STORM_WARNING);

		// 设置经纬度信息
        setLocation.add(WeatherConstant.EXTREME_WEATHER_WARNING);
        setLocation.add(WeatherConstant.SEA_FOG);
        setLocation.add(WeatherConstant.SEA_CONDITIONS);

        setEventId.add(WeatherConstant.TSUNAMI_WARNING_DETAIL);

        setYear.add(WeatherConstant.TROPICAL_CYCLONE_YEAR);
        setCode.add(WeatherConstant.TROPICAL_CYCLONE_FORECAST);
        setCode.add(WeatherConstant.TROPICAL_CYCLONE_TRACK);
        /**
         * 两个参数
         */
        setSpaceAndBegin.add(WeatherConstant.SEVENTY_TWO_HOURS);
        setSpaceAndBegin.add(WeatherConstant.SEVENTY_TWO_HOURS_TIDAL);
        setSpaceAndBegin.add(WeatherConstant.SEVEN_DAY_FORECAST);
    }

	public static void main(String[] args){
		String weather = WeatherUtil.getWeather(WeatherConstant.SEVEN_DAY_FORECAST,"120.098101,38.595696");
		System.out.println(weather);
	}
}
